//檢查客戶姓名 重復提示：繼續輸入或查看（當前頁面）
function checkNameUnique(name){
	if(name==''){return;}
	$.ajax({
		type:'post',
		url:'checkNameUnique',
		dataType:'json',
		data:{name:name},
		success:uniqueNameStatus,
		error:function(){alert("error");}
	});
}
function uniqueNameStatus(data){
	if(data.status===1){
		if(confirm('該客戶姓名已經存在，是否帶入資料？')){
			var user = new Array();
			$.each(data.data,function(key,value){ user[key] = value; });				
			$('#cu_name').val(user['cu_name']);
			$('#cu_recom').val(user['cu_recom']);
			$('#cu_impor').val(user['cu_impor']);
			$('#cu_nation').val(user['cu_nation']);				
			$('#cu_en').val(user['cu_en']);
			if(user['cu_sex']=='1'){ $('#cu_sex1').attr('checked','checked'); }else if(user['cu_sex']=='0'){ $('#cu_sex0').attr('checked','checked'); }
			$('#cu_source').val(user['cu_source']);
			
			$('#cu_mobile').val(user['cu_mobile']);
			if(user['cu_sms']=='Y'){	$('#cu_smsy').attr('checked','checked');	$('#cu_smsn').attr('checked','');}
			else if(user['cu_sms']=='N'){	$('#cu_smsy').attr('checked','');	$('#cu_smsn').attr('checked','checked');	}
			if(user['cu_mobile1']){
				if($('#1').attr('id')!=1){ addEle_Mobile1();	}
				$('#cu_mobile1').val(user['cu_mobile1']);
				if(user['cu_sms1']=='Y'){	$('#cu_sms1y').attr('checked','checked');	$('#cu_sms1n').attr('checked','');
				}else if(user['cu_sms1']=='N'){	$('#cu_sms1y').attr('checked','');	$('#cu_sms1n').attr('checked','checked');	}
			}
			if(user['cu_mobile2']){
				if($('#2').attr('id')!=2){ addEle_Mobile2();	}
				$('#cu_mobile2').val(user['cu_mobile2']);
				if(user['cu_sms2']=='Y'){	$('#cu_sms2y').attr('checked','checked');	$('#cu_sms2n').attr('checked','');
				}else if(user['cu_sms1']=='N'){	$('#cu_sms2y').attr('checked','');	$('#cu_sms2n').attr('checked','checked');}
			}
			
			$('#cu_htel').val(user['cu_htel']);
			if(user['cu_htel1']){ if($('#3').attr('id')!=3){ addEle_hometel1(); } $('#cu_htel1').val(user['cu_htel1']); }
			if(user['cu_htel2']){ if($('#4').attr('id')!=4){ addEle_hometel2(); } $('#cu_htel2').val(user['cu_htel2']); }
			
			$('#cu_ctel').val(user['cu_ctel']);	$('#cu_ext').val(user['cu_ext']);
			if(user['cu_ctel1']){ if($('#5').attr('id')!=5){ addEle_company_tel(); } $('#cu_ctel1').val(user['cu_ctel1']); $('#cu_ext1').val(user['cu_ext1']); }
			if(user['cu_ctel2']){ if($('#6').attr('id')!=6){ addEle_company_tel(); } $('#cu_ctel2').val(user['cu_ctel2']); $('#cu_ext2').val(user['cu_ext2']); }

			$('#cu_mail').val(user['cu_mail']);
			if(user['cu_edm']=='Y'){	$('#cu_edmy').attr('checked','checked');	$('#cu_edmn').attr('checked','');}
			else if(user['cu_edm']=='N'){	$('#cu_edmy').attr('checked','');	$('#cu_edmn').attr('checked','checked');	}
			if(user['cu_mail1']){
				if($('#7').attr('id')!=7){ addEle_email1(); }
				$('#cu_mail1').val(user['cu_mail1']);
				if(user['cu_edm1']=='Y'){	$('#cu_edm1y').attr('checked','checked');	$('#cu_edm1n').attr('checked','');
				}else if(user['cu_edm1']=='N'){	$('#cu_edm1y').attr('checked','');	$('#cu_edm1n').attr('checked','checked');	}
			}
			if(user['cu_mail2']){
				if($('#8').attr('id')!=8){ addEle_email2(); }
				$('#cu_mail2').val(user['cu_mail2']);
				if(user['cu_edm2']=='Y'){	$('#cu_edm2y').attr('checked','checked');	$('#cu_edm2n').attr('checked','');
				}else if(user['cu_edm2']=='N'){	$('#cu_edm2y').attr('checked','');	$('#cu_edm2n').attr('checked','checked');	}
			}
			
			$('#cu_fax').val(user['cu_fax']);
			if(user['cu_fax1']){ if($('#9').attr('id')!=9){ addEle_fax1(); } $('#cu_fax1').val(user['cu_fax1']); }
			if(user['cu_fax2']){ if($('#10').attr('id')!=10){ addEle_fax2(); } $('#cu_fax2').val(user['cu_fax2']); }
			
			$('#cu_hadd').val(user['cu_hadd']);	$('#cu_hbuild').val(user['cu_hbuild']);
			if(user['cu_hpubl']=='Y'){	$('#cu_hpubly').attr('checked','checked');	$('#cu_hpubln').attr('checked','');}
			else if(user['cu_hpubl']=='N'){	$('#cu_hpubly').attr('checked','');	$('#cu_hpubln').attr('checked','checked');	}
			if(user['cu_hadd1']){
				if($('#11').attr('id')!=11){ addEle_address1();	}
				$('#cu_hadd1').val(user['cu_hadd1']); $('#cu_hbuild1').val(user['cu_hbuild1']);
				if(user['cu_hpubl1']=='Y'){	$('#cu_hpubl1y').attr('checked','checked');	$('#cu_hpubl1n').attr('checked','');
				}else if(user['cu_hpubl1']=='N'){	$('#cu_hpubl1y').attr('checked','');	$('#cu_hpubl1n').attr('checked','checked');	}
			}
			if(user['cu_hadd2']){
				if($('#12').attr('id')!=12){ addEle_address2();	}
				$('#cu_hadd2').val(user['cu_hadd2']); $('#cu_hbuild2').val(user['cu_hbuild2']);
				if(user['cu_hpubl2']=='Y'){	$('#cu_hpubl2y').attr('checked','checked');	$('#cu_hpubl2n').attr('checked','');
				}else if(user['cu_hpubl2']=='N'){ $('#cu_hpubl2y').attr('checked',''); $('#cu_hpubl2n').attr('checked','checked');	}
			}
		
			$('#cu_company').val(user['cu_company']);	$('#cu_type').val(user['cu_type']);		$('#uniform').val(user['cu_unified']);
			$('#cu_dep').val(user['cu_dep']);			$('#cu_titles').val(user['cu_titles']);	$('#cu_cate').val(user['cu_cate']);				
			if(user['cu_company1']){
				if($('#13').attr('id')!=13){ addEle_company1(); }
				$('#cu_company1').val(user['cu_company1']);	$('#cu_type1').val(user['cu_type1']);		$('#cu_unified1').val(user['cu_unified1']);
				$('#cu_dep1').val(user['cu_dep1']);			$('#cu_titles1').val(user['cu_titles1']);	$('#cu_cate1').val(user['cu_cate1']);	
			}
			if(user['cu_company2']){
				if($('#14').attr('id')!=14){ addEle_company2(); }
				$('#cu_company2').val(user['cu_company2']);	$('#cu_type2').val(user['cu_type2']);		$('#cu_unified2').val(user['cu_unified2']);
				$('#cu_dep2').val(user['cu_dep2']);			$('#cu_titles2').val(user['cu_titles2']);	$('#cu_cate2').val(user['cu_cate2']);	
			}			
			//公司地址
			$('#cu_cadd').val(user['cu_cadd']); $('#cu_cbuild').val(user['cu_cbuild']); $('#cu_web').val(user['cu_web']);
			if(user['cu_cpubl']=='Y'){	$('#cu_cpubly').attr('checked','checked');	$('#cu_cpubln').attr('checked','');	}
			else if(user['cu_cpubl']=='N'){	$('#cu_cpubly').attr('checked','');	$('#cu_cpubln').attr('checked','checked'); }
			if(user['cu_cadd1']){
				if($('#15').attr('id')!=15){ addEle_company_addr1(); }
				$('#cu_cadd1').val(user['cu_cadd1']);	$('#cu_cbuild1').val(user['cu_cbuild1']);		$('#cu_web1').val(user['cu_web1']);
				if(user['cu_cpubl1']=='Y'){	$('#cu_cpubl1y').attr('checked','checked');	$('#cu_cpubl1n').attr('checked','');	}
				else if(user['cu_cpubl1']=='N'){	$('#cu_cpubl1y').attr('checked','');	$('#cu_cpubl1n').attr('checked','checked'); }	
			}
			if(user['cu_cadd2']){
				if($('#16').attr('id')!=16){ addEle_company_addr2(); }
				$('#cu_cadd2').val(user['cu_cadd2']);	$('#cu_cbuild2').val(user['cu_cbuild2']);		$('#cu_web2').val(user['cu_web2']);
				if(user['cu_cpubl2']=='Y'){	$('#cu_cpubl2y').attr('checked','checked');	$('#cu_cpubl2n').attr('checked','');	}
				else if(user['cu_cpubl2']=='N'){	$('#cu_cpubl2y').attr('checked','');	$('#cu_cpubl2n').attr('checked','checked'); }	
			}
			
			$('#cu_birthday').val(user['cu_birthday']); $('#cu_id').val(user['cu_id']); $('#cu_charac').val(user['cu_charac']);
			if(user['cu_gifts']=='Y'){	$('#cu_giftsy').attr('checked','checked');	$('#cu_giftsn').attr('checked','');	}
			else if(user['cu_gifts']=='N'){	$('#cu_giftsy').attr('checked','');	$('#cu_giftsn').attr('checked','checked'); }
			
			$('#cu_week').val(user['cu_week']); $('#cu_week_start').val(user['cu_week_start']); $('#cu_week_end').val(user['cu_week_end']);
			if(user['cu_week1']){
				if($('#17').attr('id')!=17){ addping1(); }
				$('#cu_week1').val(user['cu_week1']);	$('#cu_week_start1').val(user['cu_week_start1']);		$('#cu_week_end1').val(user['cu_week_end1']);
			}
			if(user['cu_week2']){
				if($('#18').attr('id')!=18){ addping2(); }
				$('#cu_week2').val(user['cu_week2']);	$('#cu_week_start2').val(user['cu_week_start2']);		$('#cu_week_end2').val(user['cu_week_end2']);
			}
			
			$('#cu_holi').val(user['cu_holi']); $('#cu_holi_start').val(user['cu_holi_start']); $('#cu_holi_end').val(user['cu_holi_end']);
			if(user['cu_holi1']){
				if($('#19').attr('id')!=19){ addholiday1(); }
				$('#cu_holi1').val(user['cu_holi1']);	$('#cu_holi_start1').val(user['cu_holi_start1']);		$('#cu_holi_end1').val(user['cu_holi_end1']);
			}
			if(user['cu_holi']){
				if($('#20').attr('id')!=20){ addholiday2(); }
				$('#cu_holi2').val(user['cu_holi2']);	$('#cu_holi_start2').val(user['cu_holi_start2']);		$('#cu_holi_end2').val(user['cu_holi_end2']);
			}
			
			$('#cu_front').val(user['cu_front']);
			$('#cu_competent').val(user['cu_competent']);
			$('#cu_broker').val(user['cu_broker']);
			$('#cu_broker1').val(user['cu_broker1']);
		}
	}
}

//顯示｜隱藏詳細訊息
function con_detail_info(){	
	var div = $("#detail_info");
	var dis = div.css('display');
	if(dis == 'none'){ div.show(); }
	else{ div.hide(); }
}

function checkform(){
	var check=$(".check").get();
	var error=0;
	$.each(check,function(key,value){
		$(this).css("border","");
		$(this).attr("title","");
		if(value["value"]===''){
			$(this).css("border","1px solid red");
			$(this).attr("title","這裡不能為空");
			error++;
		}
	});
	
	if(error===0){
		if($("#unique_info").attr("status")==='true'){
			$(".tips").html("<span style='font-size:30px;color:green;'>√</span>");
			$("#userinfo").submit();
		}
			
	}else{
		$(".tips").html("<span style='font-size:30px;color:red;'>注：紅色邊框的輸入框為必填項</span>");
	}
}

//mobile start
function addEle_Mobile(){	
	var li1 = $('#1').attr('id');
	var li2 = $('#2').attr('id');
	if(li1 != 1 && li2 !=2){ addEle_Mobile1(); }
	else if(li1 == 1 && li2 !=2){ addEle_Mobile2(); }
	else if(li1 == 1 && li2 ==2){ alert("最多只能添加3個哦");return; }
}
function addEle_Mobile1(){
	$("#add_tel").append("<li id='1'><input type='text' name='cu_mobile1' id='cu_mobile1' class='mobile' /><a onclick='removeEle_Mobile(1)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$("#mobilesms").append("<li id='1'><input type='radio' name='cu_sms1' id='cu_sms1y' value='Y' checked/>Y&nbsp;<input type='radio' name='cu_sms1' id='cu_sms1n' value='N'/>N</li>");
}
function addEle_Mobile2(){
	$("#add_tel").append("<li id='2'><input type='text' name='cu_mobile2' id='cu_mobile2' class='mobile' /><a onclick='removeEle_Mobile(2)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$("#mobilesms").append("<li id='2'><input type='radio' name='cu_sms2' id='cu_sms2y' value='Y' checked/>Y&nbsp;<input type='radio' name='cu_sms2' id='cu_sms2n' value='N'/>N</li>");
}
function removeEle_Mobile(item){	$("#"+item).remove();	$("#"+item).remove();	}
//mobile end

//home_tel start
function addEle_hometel(){
	var li1 = $('#3').attr('id');
	var li2 = $('#4').attr('id');
	if(li1 != 3 && li2 !=4){ addEle_hometel1(); }
	else if(li1 == 3 && li2 !=4){ addEle_hometel2(); }
	else if(li1 == 3 && li2 ==4){ alert("最多只能添加3個哦");return; }
}
function addEle_hometel1(){
	$("#home_tel").append("<li id='3'><input type='text' class='colspan_input1' name='cu_htel1' id='cu_htel1'/><a onclick='removeEle_hometel(3)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function addEle_hometel2(){
	$("#home_tel").append("<li id='4'><input type='text' class='colspan_input1' name='cu_htel2' id='cu_htel2'/><a onclick='removeEle_hometel(4)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function removeEle_hometel(item){	$("#"+item).remove();	}
//home_tel end

//company_tel start
function addEle_company_tel(){
	var li1 = $('#5').attr('id');
	var li2 = $('#6').attr('id');
	if(li1 != 5 && li2 !=6){ addEle_company_tel1(); }
	else if(li1 == 5 && li2 !=6){ addEle_company_tel2(); }
	else if(li1 == 5 && li2 ==6){ alert("最多只能添加3個哦");return; }
}
function addEle_company_tel1(){
	$("#company_tel").append("<li id='5'><input type='text' name='cu_ctel1' id='cu_ctel1' class='colspan_input1' />&nbsp;&nbsp;<b>分機</b>&nbsp;&nbsp;<input type='text' class='c_tel_2' name='cu_ext1' id='cu_ext1' ><a onclick='removeEle_company_tel(5)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function addEle_company_tel2(){
	$("#company_tel").append("<li id='6'><input type='text' name='cu_ctel2' id='cu_ctel2' class='colspan_input1' />&nbsp;&nbsp;<b>分機</b>&nbsp;&nbsp;<input type='text' class='c_tel_2' name='cu_ext2' id='cu_ext2' ><a onclick='removeEle_company_tel(6)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function removeEle_company_tel(item){ $("#"+item).remove();	}
//company_tel end

//email start
function addEle_email(){
	var li1 = $('#7').attr('id');
	var li2 = $('#8').attr('id');
	if(li1 != 7 && li2 !=8){ addEle_email1(); }
	else if(li1 == 7 && li2 !=8){ addEle_email2(); }
	else if(li1 == 7 && li2 ==8){ alert("最多只能添加3個哦");return; }
}
function addEle_email1(){
	var radio="<input type='radio' name='cu_edm1' id='cu_edm1y' value='Y' checked='checked' />Y&nbsp;&nbsp;<input type='radio' name='cu_edm1' id='cu_edm1n' value='N'/>N";
	$("#email").append("<li id='7'><input type='text' name='cu_mail1' id='cu_mail1' class='email_input' /><input type='button' value='開啟mail' onclick=open_email('cu_mail1') /><a onclick='removeEle_email(7)' class='deleteitem' ><i class='icon-trash'></i></a></li>");
	$("#email_edm").append("<li class='7'>"+radio+"</li>");
}
function addEle_email2(){
	var radio="<input type='radio' name='cu_edm2' id='cu_edm2y' value='Y' checked='checked' />Y&nbsp;&nbsp;<input type='radio' name='cu_edm2' id='cu_edm2n' value='N'/>N";
	$("#email").append("<li id='8'><input type='text' name='cu_mail2' id='cu_mail2' class='email_input' /><input type='button' value='開啟mail' onclick=open_email('cu_mail2') /><a onclick='removeEle_email(8)' class='deleteitem' ><i class='icon-trash'></i></a></li>");
	$("#email_edm").append("<li class='8'>"+radio+"</li>");
}
function removeEle_email(item){	$("#"+item).remove();	$("."+item).remove();	}
//email end

//fax start
function addEle_fax(){
	var li1 = $('#9').attr('id');
	var li2 = $('#10').attr('id');
	if(li1 != 9 && li2 !=10){ addEle_fax1(); }
	else if(li1 == 9 && li2 !=10){ addEle_fax2(); }
	else if(li1 == 9 && li2 ==10){ alert("最多只能添加3個哦");return; }
}
function addEle_fax1(){
	$("#fax").append("<li id='9'><input type='text' name='cu_fax1' id='cu_fax1' class='colspan_input1' /><a onclick='removeEle_fax(9)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function addEle_fax2(){
	$("#fax").append("<li id='10'><input type='text' name='cu_fax2' id='cu_fax2' class='colspan_input1' /><a onclick='removeEle_fax(10)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function removeEle_fax(item){	$("#"+item).remove();	}
//fax end

//homeaddress start
function addEle_address(){
	var li1 = $('#11').attr('id');
	var li2 = $('#12').attr('id');
	if(li1 != 11 && li2 !=12){ addEle_address1(); }
	else if(li1 == 11 && li2 !=12){ addEle_address2(); }
	else if(li1 == 11 && li2 ==12){ alert("最多只能添加3個哦");return; }
}
function addEle_address1(){
	var build_name=$("#cu_hbuild").html();
	var radio="<input type='radio' name='cu_hpubl1' id='cu_hpubl1y' value='Y' checked/>Y&nbsp;&nbsp;<input type='radio' name='cu_hpubl1' id='cu_hpubl1n' value='N'/>N";
	$("#home_addr").append("<li id='11'><input type='text' class='colspan_input' name='cu_hadd1' id='cu_hadd1' onClick=showFamilyAddress('cu_hadd1'); /><a onclick='removeEle_address(11)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$("#building_name").append("<li class='11'><select name='cu_hbuild1' id='cu_hbuild1' class='full_select' >"+build_name+"</select></li>");
	$("#per").append("<li class='11'>"+radio+"</li>");
}
function addEle_address2(){
	var build_name=$("#cu_hbuild").html();
	var radio="<input type='radio' name='cu_hpubl2' id='cu_hpubl2y' value='Y' checked/>Y&nbsp;&nbsp;<input type='radio' name='cu_hpubl2' id='cu_hpubl2n' value='N'/>N";
	$("#home_addr").append("<li id='12'><input type='text' class='colspan_input' name='cu_hadd2' id='cu_hadd2' onClick=showFamilyAddress('cu_hadd2'); /><a onclick='removeEle_address(12)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$("#building_name").append("<li class='12'><select name='cu_hbuild1' id='cu_hbuild2' class='full_select' >"+build_name+"</select></li>");
	$("#per").append("<li class='12'>"+radio+"</li>");
}
function removeEle_address(item){	$("#"+item).remove();	$("."+item).remove();	}
//homeaddress end

//company start
function addEle_company(){
	var li1 = $('#13').attr('id');
	var li2 = $('#14').attr('id');
	if(li1 != 13 && li2 !=14){ addEle_company1(); }
	else if(li1 == 13 && li2 !=14){ addEle_company2(); }
	else if(li1 == 13 && li2 ==14){ alert("最多只能添加3個哦");return; }
}
function addEle_company1(){
	var utype=$("#cu_cate").html();
	var cu_titles=$("#cu_titles").html();
	var ctype=$("#cu_type").html();
	var select2="<select name='cu_cate1' id='cu_cate1' style='width:154px;' >"+utype+"</select>";
	$("#company_name").append("<li id='13'><input type='text' class='colspan_input' id='cu_company1' onkeyup='completInfo1()' name='cu_company1' /><a onclick='removeEle_company(13)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$('#company_type').append("<li class='13'><select name='cu_type1' id='cu_type1' class='full_select' >"+ctype+"</select></li>");
	$("#company_id").append("<li class='13'><input type='text' name='cu_unified1' id='cu_unified1' style='width:148px;' onblur='checkuniformcode(this);' /></li>");
	$("#dept").append("<li class='13'><input type='text' id='cu_dep1' name='cu_dep1' /></li>");
	$("#zhichen").append("<li class='13'><select name='cu_titles1' id='cu_titles1' style='width:245px;'>"+cu_titles+"</select></li>");
	$('#company_type2').append("<li class='13'>"+select2+"</li>");
}
function addEle_company2(){
	var utype=$("#cu_cate").html();
	var cu_titles=$("#cu_titles").html();
	var ctype=$("#cu_type").html();
	var select2="<select name='cu_cate2' id='cu_cate2' style='width:154px;' >"+utype+"</select>";
	$("#company_name").append("<li id='14'><input type='text' class='colspan_input' id='cu_company2' onkeyup='completInfo1()' name='cu_company2' /><a onclick='removeEle_company(14)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$('#company_type').append("<li class='14'><select name='cu_type2' id='cu_type2' class='full_select' >"+ctype+"</select></li>");
	$("#company_id").append("<li class='14'><input type='text' name='cu_unified2' id='cu_unified2' style='width:148px;' onblur='checkuniformcode(this);' /></li>");
	$("#dept").append("<li class='14'><input type='text' id='cu_dep2' name='cu_dep2' /></li>");
	$("#zhichen").append("<li class='14'><select name='cu_titles2' id='cu_titles2' style='width:245px;'>"+cu_titles+"</select></li>");
	$('#company_type2').append("<li class='14'>"+select2+"</li>");
}
function completInfo1(){
	cu_company1 = $("#cu_company1").val();
	$.ajax({
		url:'companyinfo_search1',
		type:'post',
		dataType:'json',
		data:{companyname:companyName},
		success:function(data){
			$.each(data.data,function(key,value){
				$("#cu_type1").val(value['cu_type1']);
				$("#uniform1").val(value['cu_unified1']); //统一编号
				$("#cu_cate1").val(value['cu_cate1']);
				$("#cu_cadd1").val(value['cu_cadd1']);
				$("#cu_cbuild1").val(value['cu_cbuild1']);
				$("#cu_web1").val(value['cu_web1']);			
			});
		}
	});
}
//公司名称带出相关信息
function completInfo(){
	companyName = $("#companyName").val();
	$.ajax({
		url:'companyinfo_search',
		type:'post',
		dataType:'json',
		data:{companyname:companyName},
		success:function(data){
			$.each(data.data,function(key,value){
				$("#cu_type").val(value['cu_type']);
				$("#uniform").val(value['cu_unified']); //统一编号
				$("#cu_cate").val(value['cu_cate']);
				$("#cu_cadd").val(value['cu_cadd']);
				$("#cu_cbuild").val(value['cu_cbuild']);
				$("#cu_web").val(value['cu_web']);			
			});
		}
	});
}
function removeEle_company(item){ $("#"+item).remove();	$("."+item).remove(); }
//company end

//company_addr start 
function addEle_company_addr(){
	var li1 = $('#15').attr('id');
	var li2 = $('#16').attr('id');
	if(li1 != 15 && li2 !=16){ addEle_company_addr1(); }
	else if(li1 == 15 && li2 !=16){ addEle_company_addr2(); }
	else if(li1 == 15 && li2 ==16){ alert("最多只能添加3個哦");return; }
}
function addEle_company_addr1(){
	var com_build_name=$("#cu_cbuild").html();
	var radio="<input type='radio' name='cu_cpubl1' id='cu_cpubl1y' value='Y' checked/>Y&nbsp;&nbsp;<input type='radio' name='cu_cpubl1' id='cu_cpubl1n' value='N'/>N";
	$("#company_addr").append("<li id='15'><input type='text' class='large_input' name='cu_cadd1' id='cu_cadd1' onClick=showFamilyAddress('cu_cadd1'); />&nbsp;<a onclick='removeEle_company_addr(15)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$("#building_company_name").append("<li class='15'><select name='cu_cbuild1' id='cu_cbuild1' class='full_select' >"+com_build_name+"</select></li>");
	$("#per_com").append("<li class='15'>"+radio+"</li>");
	$("#company_website").append("<li class='15'><input type='text' name='cu_web1' style='width:374px;' id='cu_web1' /><input type='button' value='開啟IE' onclick=open_new_window('cu_web1'); /></li>");
}
function addEle_company_addr2(){
	var com_build_name=$("#cu_cbuild").html();
	var radio="<input type='radio' name='cu_cpubl2' id='cu_cpubl2y' value='Y' checked/>Y&nbsp;&nbsp;<input type='radio' name='cu_cpubl2' id='cu_cpubl2n' value='N'/>N";
	$("#company_addr").append("<li id='16'><input type='text' class='large_input' name='cu_cadd2' id='cu_cadd2' onClick=showFamilyAddress('cu_cadd2'); />&nbsp;<a onclick='removeEle_company_addr(16)' class='deleteitem'><i class='icon-trash'></i></a></li>");
	$("#building_company_name").append("<li class='16'><select name='cu_cbuild2' id='cu_cbuild2' class='full_select' >"+com_build_name+"</select></li>");
	$("#per_com").append("<li class='16'>"+radio+"</li>");
	$("#company_website").append("<li class='16'><input type='text' name='cu_web2' style='width:374px;' id='cu_web2' /><input type='button' value='開啟IE' onclick=open_new_window('cu_web2'); /></li>");
}
function removeEle_company_addr(item){	$("#"+item).remove(); $("."+item).remove(); }
//company_addr end

//平日连络start
function addping(){
	var li1 = $('#17').attr('id');
	var li2 = $('#18').attr('id');
	if(li1 != 17 && li2 !=18){ addping1(); }
	else if(li1 == 17 && li2 !=18){ addping2(); }
	else if(li1 == 17 && li2 ==18){ alert("最多只能添加3個哦");return; }
}
function addping1(){
	var data = $("#cu_week").html();
	var str_sm = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;起&nbsp;<input type='text' name='cu_week_start1' id='cu_week_start1' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	str_sm += "&nbsp;&nbsp;&nbsp;&nbsp;止&nbsp;<input type='text' name='cu_week_end1' id='cu_week_end1' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	$("#ping").append("<li id='17' class='ping_li'><select id='cu_week1' name='cu_week1' style='width:70px' >"+data+"</select>"+str_sm+"&nbsp;<a onclick='removeping(17)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function addping2(){
	var data = $("#cu_week").html();
	var str_sm = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;起&nbsp;<input type='text' name='cu_week_start2' id='cu_week_start2' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	str_sm += "&nbsp;&nbsp;&nbsp;&nbsp;止&nbsp;<input type='text' name='cu_week_end2' id='cu_week_end2' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	$("#ping").append("<li id='18' class='ping_li'><select id='cu_week2' name='cu_week2' style='width:70px' >"+data+"</select>"+str_sm+"&nbsp;<a onclick='removeping(18)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function removeping(item){ $("#"+item).remove(); }
//平日连络end

//假日連絡start
function addholiday(){
	var li1 = $('#19').attr('id');
	var li2 = $('#20').attr('id');
	if(li1 != 19 && li2 !=20){ addholiday1(); }
	else if(li1 == 19 && li2 !=20){ addholiday2(); }
	else if(li1 == 19 && li2 ==20){ alert("最多只能添加3個哦");return; }
}
function addholiday1(){
	var data=$("#cu_holi").html();
	var str_sm = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;起&nbsp;<input type='text' name='cu_holi_start1' id='cu_holi_start1' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	str_sm += "&nbsp;&nbsp;&nbsp;&nbsp;止&nbsp;<input type='text' name='cu_holi_end1' id='cu_holi_end1' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	$("#holiday").append("<li id='19' class='holiday_li'><select name='cu_holi1' id='cu_holi1' style='width:70px;'>"+data+"</select>"+str_sm+"&nbsp;<a onclick='removeholiday(19)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function addholiday2(){
	var data=$("#cu_holi").html();
	var str_sm = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;起&nbsp;<input type='text' name='cu_holi_start2' id='cu_holi_start2' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	str_sm += "&nbsp;&nbsp;&nbsp;&nbsp;止&nbsp;<input type='text' name='cu_holi_end2' id='cu_holi_end2' class='Wdate' onFocus=\"WdatePicker({dateFmt:'HH:mm'})\" style='width:70px;' />";
	$("#holiday").append("<li id='20' class='holiday_li'><select name='cu_holi2' id='cu_holi2' style='width:70px;'>"+data+"</select>"+str_sm+"&nbsp;<a onclick='removeholiday(20)' class='deleteitem'><i class='icon-trash'></i></a></li>");
}
function removeholiday(item){ $("#"+item).remove(); }
//假日連絡end

function open_new_window(id){
	var url=$("#"+id).val();
	if(url!=''){
		var regexp=/.*https?:\/\/*/;
		if(regexp.test(url)){
			window.open(url,"newwindow","resizable=yes,location=no");
		}else{
			var regexp=/www\..*/;
			if(regexp.test(url)){
				window.open('http://'+url,"newwindow","resizable=yes,location=no");
			}else{
				alert("網址必須以www開頭");
			}
		}
	}else{
		alert("網址不能為空");
	}
}

function lookforco(val){
	if(val.length == 0){
		$('#showco').hide();
	}else{
		$.ajax({
			url:'showSearchCompany',
			type:'post',
			data:{name:val},
			dataType:'json',
			success:function(data){
				if(data.status === 1){
					$('#showco').show();
					$('#showco').html(data.data);
				}
			},
			error:function(){alert('error');}
		});
	}
}
function fillco(v){
	$('#companyName').val(v);
	setTimeout("$('#showco').hide();",100);
}


